<?php
/*
 * program:	均源
 * author:	雨中漫步 & 上善若水 & qhxin
 * email:	qhxin@qhxin.com
 * date:	2014/11/11
 * description:	又是一年双十一，不能剁手，就写点东西吧~~~~~~
 */

defined('IN_SCHOOL') or die();

/*{TranslateTagBegin}*/
class control_simpost extends base_control{

	//hook ctr_simpost_start.php
	private $do_more;

	public function __construct() {
		//hook ctr_simpost_init_init_before.php
		parent::__construct();
		
		$this->do_more = false;
		//hook ctr_simpost_init_init_end.php
	}
	
	public function do_index() {
		//hook ctr_simpost_index_start.php
		$this->message('参数错误', 'index.php');
	}
	
	public function do_subreply() {
		$fid = intval(core::gpc('fid','G'));
		$uname = base64_decode(core::gpc('uname','G'));
		
		//hook ctr_simpost_subreply_start.php
		//层
		$comm = $this->db->get('comm-cid-'.$fid);
		if(empty($comm)) $this->message('回复不存在', 'index.php');
		$comm['content'] = misc::utf8_sub_str($comm['content'],0,20);
			
		$the_talk = $this->talks->talks_show($comm['fid']);
		if(empty($the_talk)) $this->message('帖子不存在', 'index.php');
		
		//板块
		//hook ctr_simpost_subreply_get_bbs_before.php
		$bbs = $this->forum->forum_get($the_talk['fid']);
		$bbs['master'] = array_filter(explode(',', $bbs['master']));
		$bbs['master'][] = 1;
		//hook ctr_simpost_subreply_author_before.php
		$author  = $this->user->get_user_by_uid($comm['uid'], false);
		if(empty($author)) $this->message('作者不存在', 'index.php');
		$isonline = $this->user->is_online($author['laststamp']);
		
		$uname = empty($uname)? $author['name'] : $uname;
		
		//hook ctr_simpost_subreply_page_before.php
		$pagesize = $limit = max(0, $this->confs['_conf_bbs_commentsize']);
		$page	= html::page('page');
		$count = max(0, $comm['tj']);
		$pages = html::pages("index.php?simpost-subreply-fid-$fid.htm", $count, $page, $pagesize);
		$start = ($page-1)*$pagesize;
		$subreplys = $this->subrpl->subrpl_list($fid, $start , $limit);
		//hook ctr_simpost_subreply_page_end.php
		
		foreach($subreplys as $each_key => &$each_rep) {
			$each_rep['user'] = $this->user->get_user_by_uid($each_rep['uid'], false);
			if(empty($each_rep['user'])) {
				unset($subreplys[$each_key]);
			}
			//hook ctr_simpost_subreply_in_each_subreply.php
			$each_rep['isonline'] = $this->user->is_online($each_rep['user']['laststamp']);
			$each_rep['content'] = nl2br($each_rep['content']);
			$this->parse->parse_face($each_rep['content']);
		}
		//hook ctr_simpost_subreply_end.php
		$this->tpl->assign('author', $author);
		$this->tpl->assign('uname', $uname);
		$this->tpl->assign('isonline', $isonline);
		$this->tpl->assign('comm', $comm);
		$this->tpl->assign('pages', $pages);
		$this->tpl->assign('bbs', $bbs);
		$this->tpl->assign('subreplys', $subreplys);
		$this->tpl->display('index_subreply.tpl');
	}
	
	//=========================>need logined
	
	public function do_atmsglist() {
		$this->check_user_login_msg();
		
		$core_arr = $this->user->user_core();
		
		//hook ctr_simpost_atmsglist_page_before.php
		$pagesize = $limit = max(0, $this->confs['_conf_bbs_pagesize']);
		$page	= html::page('page');
		$count = $this->atmsg->atmsg_count_all($core_arr['uid']);
		$pages = html::pages("index.php?simpost-atmsglist.htm", $count, $page, $pagesize);
		$start = ($page-1)*$pagesize;
		$atmsgs = $this->atmsg->atmsg_list($core_arr['uid'], $start, $limit);
		
		//hook ctr_simpost_atmsglist_page_end.php
		$this->tpl->assign('atmsgs', $atmsgs);
		$this->tpl->assign('pages', $pages);
		$this->tpl->display('user_atmsglist.tpl');
	}
	
	//=========================>more add
	public function do_talkadd() {
		$this->check_user_login_msg();
		$content = core::gpc('content','P');
		$fid = core::gpc('fid','R');
		$title = core::gpc('title','P');
		
		//hook ctr_simpost_talkadd_parse_before.php
		$this->parse->parse_title($title);
		$this->parse->parse_content($content, false);
		//hook ctr_simpost_talkadd_parse_end.php
		
		$talksub = core::gpc('talksub', 'P');
		$addface = core::gpc('addface', 'P');
		$addfile = core::gpc('addfile', 'P');
		$addimgs = core::gpc('addimgs', 'P');
		
		$show_face_html = false;
		$show_file_html = false;
		$show_imgs_html = false;
		$show_face_num = 1;
		$alert_msg = '';
		$face_name = $this->parse->parse_facename();
		$done = 0;
		if($addface != NULL) $done += 1;
		if($addfile != NULL) $done += 10;
		if($addimgs != NULL) $done += 50;
		if($talksub != NULL) $done += 100;
		//hook ctr_simpost_talkadd_switch_before.php
		
		switch($done) {
		case 1:
			$facecode = core::gpc('facecode', 'P');
			if(empty($facecode)) {
				$show_face_html = true;
			}else{
				$content .= "{JYFACE:$facecode}";
				//hook ctr_simpost_talkadd_face_add.php
			}
			break;
		case 10:
			$show_file_html = true;
			if(html::form_submit( CONFIG_CS_KEY )) {
				//hook ctr_simpost_talkadd_upfile_before.php
				$this->upfile->upload();
				if($this->upfile->error_code == 0) {
					//hook ctr_simpost_talkadd_upfile_no_err.php
					$uid = $this->this_user->user_uid();
					$md5 = md5($this->upfile->upload_path);
					$upinfo = array();
					$upinfo['fname'] = utf8::safe_substr($this->upfile->file_info['name'], 0, 60);
					$upinfo['fsize'] = abs(filesize($this->upfile->upload_path));
					//hook ctr_simpost_talkadd_before_jyfile_add.php
					if($this->attach->attach_add($uid,$this->upfile->upload_path,
							$md5, core::json_encode($upinfo) )){
						$show_file_html = false;
						$content .= "{JYFILE:$md5}";
						//hook ctr_simpost_talkadd_jyfile_add.php
					}else{
						@unlink($this->upfile->upload_path);
						$alert_msg = '保存附件失败';
					}
				}else{
					//hook ctr_simpost_talkadd_upfile_err.php
					$alert_msg = $this->miscfunc->check_upload_err($this->upfile->error_code);
				}
			}else{
				$alert_msg = '附件大小不能超过'.misc::humansize($this->confs['_file_allow_size'])
								.'<br/>允许格式：';
				foreach($this->confs['_file_file_ext'] as $k=>$v)
					$alert_msg .= ".$v ";
				
				//hook ctr_simpost_talkadd_before_upfile.php
			}
			break;
		case 50:
			$show_imgs_html = true;
			if(html::form_submit( CONFIG_CS_KEY )) {
				//hook ctr_simpost_talkadd_upimg_before.php
				$this->upimg->upload();
				if($this->upimg->error_code == 0) {
					//hook ctr_simpost_talkadd_upimg_no_err.php
					$image_size = getimagesize($this->upimg->upload_path);
					if($image_size == false) {
						@unlink($this->upimg->upload_path);
						$alert_msg = '图像格式及签名不正确';
					}
					if($alert_msg == '' && $image_size[0]*$image_size[1] > 1000000) {
						@unlink($this->upimg->upload_path);
						$alert_msg = '分辨率不能超过1000*1000';
					}
					$thumb_width = 200;
					$thumb_height = 200;
					//hook ctr_simpost_talkadd_upimg_start_thumb.php
					if($alert_msg == '' && ($image_size[0]>$thumb_width || $image_size[1]>$thumb_height)) {
						$thum_name = $this->parse->parse_thumb_name($this->upimg->upload_path);
						//hook ctr_simpost_talkadd_upimg_before_thumb.php
						$this->rzimg->resize_img($this->upimg->upload_path, $thumb_width, $thumb_height, 1,
										CONFIG_THU_PATH, $thum_name);
						if(!file_exists($this->rzimg->result_img)) {
							@unlink($this->upimg->upload_path);
							$alert_msg = '生成缩略图失败';
						}
					}
					if($alert_msg == '') {
						$uid = $this->this_user->user_uid();
						$md5 = md5($this->upimg->upload_path);
						$upinfo = array();
						$upinfo['fname'] = utf8::safe_substr($this->upimg->file_info['name'], 0, 60);
						$upinfo['fsize'] = abs(filesize($this->upimg->upload_path));
						//hook ctr_simpost_talkadd_before_jyimgs_add.php
						if($this->attach->attach_add($uid,$this->upimg->upload_path,
								$md5, core::json_encode($upinfo) )){
							$show_imgs_html = false;
							$content .= "{JYIMGS:$md5}";
							//hook ctr_simpost_talkadd_upimg_jyimgs_add.php
						}else{
							@unlink($this->upimg->upload_path);
							$alert_msg = '保存图片失败';
						}
					}
				}else{
					//hook ctr_simpost_talkadd_upimg_err.php
					$alert_msg = $this->miscfunc->check_upload_err($this->upimg->error_code);
				}
			}else{
				$alert_msg = '图片大小不能超过'.misc::humansize($this->confs['_file_allow_size'])
				.'<br/>分辨率不能超过1000*1000'
				.'<br/>允许格式：';
				foreach($this->confs['_file_imgs_ext'] as $k=>$v)
					$alert_msg .= ".$v ";
				
				//hook ctr_simpost_talkadd_before_upimg.php
			}
			break;
		case 100:
			$this->do_more = true;
			//hook ctr_simpost_talkadd_before_simp_talkadd.php
			$this->do_simptalkadd(); break;
		//hook ctr_simpost_talkadd_more_switch.php
		default:
			break;
		}
		
		$FORM_HASH = html::form_hash( CONFIG_CS_KEY );
		
		//hook ctr_simpost_talkadd_end.php
		$this->tpl->assign('FORM_HASH', $FORM_HASH);
		$this->tpl->assign('fid', $fid);
		$this->tpl->assign('content', $content);
		$this->tpl->assign('title', $title);
		$this->tpl->assign('show_face_html', $show_face_html);
		$this->tpl->assign('show_file_html', $show_file_html);
		$this->tpl->assign('show_imgs_html', $show_imgs_html);
		$this->tpl->assign('face_name', $face_name);
		$this->tpl->assign('alert_msg', $alert_msg);
		$this->tpl->assign('show_face_num', $show_face_num);
		$this->tpl->display('post_talkadd.tpl');
	}
	
	public function do_commadd() {
		$this->check_user_login_msg();
		
		$content = core::gpc('content','P');
		$fid = core::gpc('fid','R');
		$author = core::gpc('author','R');
		if(!empty($author)) $content = '@'.base64_decode($author).' ';
		
		//hook ctr_simpost_commadd_parse_before.php
		$this->parse->parse_content($content, false);
		//hook ctr_simpost_commadd_parse_end.php
		
		$talksub = core::gpc('talksub', 'P');
		$addface = core::gpc('addface', 'P');
		
		$show_face_html = false;
		$show_face_num = 1;
		$face_name = $this->parse->parse_facename();
		$done = 0;
		if($addface != NULL) $done += 1;
		if($talksub != NULL) $done += 100;
		//hook ctr_simpost_commadd_switch_before.php
		
		switch($done) {
		case 1:
			$facecode = core::gpc('facecode', 'P');
			if(empty($facecode)) {
				$show_face_html = true;
			}else{
				$content .= "{JYFACE:$facecode}";
				//hook ctr_simpost_commadd_add_jyface_end.php
			}
			break;
		case 100:
			//hook ctr_simpost_commadd_before_simp_commadd.php
			$this->do_simpcommadd(); break;
		//hook ctr_simpost_commadd_more_switch.php
		default:
			break;
		}
		
		//hook ctr_simpost_commadd_end.php
		$this->tpl->assign('fid', $fid);
		$this->tpl->assign('content', $content);
		$this->tpl->assign('show_face_html', $show_face_html);
		$this->tpl->assign('face_name', $face_name);
		$this->tpl->assign('show_face_num', $show_face_num);
		$this->tpl->display('post_commadd.tpl');
	}
	
	public function do_subadd() {
		$this->check_user_login_msg();
		
		$content = core::gpc('content','P');
		$fid = core::gpc('fid','R');
		$author = core::gpc('author','R');
		if(!empty($author)) $content = '@'.base64_decode($author).' ';
		
		//hook ctr_simpost_subadd_parse_before.php
		$this->parse->parse_content($content, false);
		//hook ctr_simpost_subadd_parse_after.php
		
		$talksub = core::gpc('talksub', 'P');
		$addface = core::gpc('addface', 'P');
		
		$show_face_html = false;
		$show_face_num = 1;
		$face_name = $this->parse->parse_facename();
		$done = 0;
		if($addface != NULL) $done += 1;
		if($talksub != NULL) $done += 100;
		//hook ctr_simpost_subadd_switch_before.php
		
		switch($done) {
		case 1:
			$facecode = core::gpc('facecode', 'P');
			if(empty($facecode)) {
				$show_face_html = true;
			}else{
				$content .= "{JYFACE:$facecode}";
				//hook ctr_simpost_subadd_add_jyface_end.php
			}
			break;
		case 100:
			//hook ctr_simpost_subadd_add_simp_subadd.php
			$this->do_simpsubadd(); break;
		//hook ctr_simpost_subadd_more_switch.php
		default:
			break;
		}
		
		//hook ctr_simpost_subadd_end.php
		$this->tpl->assign('fid', $fid);
		$this->tpl->assign('content', $content);
		$this->tpl->assign('show_face_html', $show_face_html);
		$this->tpl->assign('face_name', $face_name);
		$this->tpl->assign('show_face_num', $show_face_num);
		$this->tpl->display('post_subadd.tpl');
	}
	//=========================>base add
	public function do_simptalkadd() {
		$this->check_user_login_msg();
		
		$fid = intval(core::gpc('fid','P'));
		$title = core::gpc('title','P');
		$content = core::gpc('content','P');
		
		//hook ctr_simpost_simptalkadd_parse_before.php
		$this->parse->parse_title($title);
		$this->parse->parse_content($content, false);
		//hook ctr_simpost_simptalkadd_parse_after.php
		
		$bbs = $this->forum->forum_get($fid);
		if(empty($bbs) || $bbs['display']) $this->message('版块已关闭', 'index.php');
		
		//hook ctr_simpost_simptalkadd_bbs_after.php
		$result = '标题和内容不能为空';
		if($fid > 0 && !empty($title) && !empty($content)) {
			$uid = $this->this_user->user_uid();
			$aname = $this->this_user->user_name();
			if($this->forum->forum_edit_tj($fid , '+1')) {
				$ttid = 0;
				//hook ctr_simpost_simptalkadd_add_talks_before.php
				if($this->talks->talks_add($fid, $uid, $title, $content, $aname,$ttid)) {
					if($this->do_more) {
						//hook ctr_simpost_simptalkadd_do_more_before.php
						$attaches = $this->parse->parse_all_image_md5($content);
						foreach($attaches as $k=>$v) {
							$this->attach->attach_edit($v, $ttid);
						}
						$attaches = $this->parse->parse_all_files_md5($content);
						foreach($attaches as $k=>$v) {
							$this->attach->attach_edit($v, $ttid);
						}
						//hook ctr_simpost_simptalkadd_do_more_end.php
					}
					$num = 3;
					//hook ctr_simpost_simptalkadd_success_before.php
					$this->user->user_core_edit('+', $num);
					$result = '发帖成功，积分+'.$num;
					$this->message($result, "index.php?index-forum-fid-$fid.htm");
				}else{
					$this->forum->forum_edit_tj($fid , '-1');
					$result = '发帖失败';
				}
			}else{
					$result = '分类错误';
			}
		}
		//hook ctr_simpost_simptalkadd_end.php
		$this->message($result , 'index.php');
	}
	
	public function do_simpcommadd() {
		$this->check_user_login_msg();
	
		$fid = intval(core::gpc('fid','P'));
		$content = core::gpc('content','P');
		
		//hook ctr_simpost_simpcommadd_parse_before.php
		$this->parse->parse_content($content);
		//hook ctr_simpost_simpcommadd_parse_after.php
		
		$result = '回复内容不能为空';
		$lc = 0;
		if($fid > 0 && !empty($content)) {
			$uid = $this->this_user->user_uid();
			if($this->talks->talks_edit_tj($fid , '+', $lc)) {
				//hook ctr_simpost_simpcommadd_comm_add_before.php
				if($this->comm->comm_add($fid, $uid, $content, $lc)) {
				
					$core_arr = $this->user->user_core();
					$users = $this->parse->parse_get_at_users_name($content);
					foreach($users as $key => $name) {
						//hook ctr_simpost_simpcommadd_check_each_atmsg.php
						if($core_arr['name'] == $name) continue;
						$the_user = $this->user->get_user_by_name($name, false);
						if(!empty($the_user)) {
							//hook ctr_simpost_simpcommadd_before_send_atmsg.php
							$url = $core_arr['name'].'：<a href="index.php?index-talk-tid-'.$fid.'.htm">'.misc::utf8_sub_str($content,0,20).'</a>';
							$this->atmsg->atmsg_add($the_user['uid'],$url);
						}
					}
					$num = 1;
					//hook ctr_simpost_simpcommadd_success_before.php
					$this->user->user_core_edit('+', $num);
					$result = '回复成功，积分+'.$num;
					$this->message($result, "index.php?index-talk-tid-$fid.htm");
				}else{
					$this->talks->talks_edit_tj($fid , '-', $lc);
					$result = '回复失败';
				}
			}else{
					$result = '帖子未找到';
			}
		}
		//hook ctr_simpost_simpcommadd_end.php
		$this->message($result, "index.php?index-talk-tid-$fid.htm");
	}
	
	public function do_simpsubadd() {
		$this->check_user_login_msg();
	
		$fid = intval(core::gpc('fid','P'));
		$content = core::gpc('content','P');
		
		//hook ctr_simpost_simpsubadd_parse_before.php
		$this->parse->parse_content($content);
		//hook ctr_simpost_simpsubadd_parse_after.php
		
		$result = '回复内容不能为空';
		if($fid > 0 && !empty($content)) {
			$uid = $this->this_user->user_uid();
			$tid = 0;
			if($this->comm->comm_edit_tj($fid , '+',$tid)) {
				//hook ctr_simpost_simpsubadd_before_subrpl_add.php
				if($this->subrpl->subrpl_add($fid, $uid, $content,$tid)) {
				
					$core_arr = $this->user->user_core();
					$users = $this->parse->parse_get_at_users_name($content);
					foreach($users as $key => $name) {
						//hook ctr_simpost_simpsubadd_check_each_atmsg.php
						if($core_arr['name'] == $name) continue;
						$the_user = $this->user->get_user_by_name($name, false);
						if(!empty($the_user)) {
							//hook ctr_simpost_simpsubadd_before_send_atmsg.php
							$url = $core_arr['name'].'：<a href="index.php?simpost-subreply-fid-'.$fid.'.htm">'.misc::utf8_sub_str($content,0,20).'</a>';
							$this->atmsg->atmsg_add($the_user['uid'],$url);
						}
					}
					$num = 1;
					//hook ctr_simpost_simpsubadd_success_before.php
					$this->user->user_core_edit('+', $num);
					$result = '回复成功，积分+'.$num;
					$this->message($result, "index.php?simpost-subreply-fid-$fid.htm");
				}else{
					$this->comm->comm_edit_tj($fid , '-');
					$result = '回复失败';
				}
			}else{
					$result = '帖子未找到';
			}
		}
		//hook ctr_simpost_simpsubadd_end.php
		$this->message($result, "index.php?simpost-subreply-fid-$fid.htm");
	}

	//hook ctr_simpost_end.php

}
/*{TranslateTagEnd}*/

?>